/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing_class;

import java.awt.Color;
import stendcontrol.DataProcessor;

/**
 *
 * @author Asko
 */
public class GUITest extends javax.swing.JFrame {

    DataProcessor dp = new DataProcessor();

    /**
     * Creates new form GUITest
     */
    public GUITest() {
        initComponents();

        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pan = new org.math.plot.Plot2DPanel();
        jButton1 = new javax.swing.JButton();
        pan2 = new org.math.plot.Plot2DPanel();
        pan3 = new org.math.plot.Plot2DPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(pan2, javax.swing.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
                    .addComponent(pan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pan3, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pan, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(pan2, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pan3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    static public double nKorr(double[] v, int t, double amn, double adsp) {
        double mn = amn;
        double dsp = adsp;
        double sum = 0;

        //t = normIndex(v, t);

        for (int i = 0; i < v.length - t; i++) {
            sum += (v[i] - mn) * (v[i + t] - mn);
        }

        return sum / ((v.length - t) * dsp);
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        double gm = 0.879;
        double k = 0, m = 4;
        double omega = 5;
        double lambda = 1;

        int valNum = 5000;
        double sampIntrv = CorrelationFunctions.findSamplingInterval(omega, 0.02, 2, lambda);

        System.out.println("Sample interval -> " + sampIntrv);


        double ordNum = CorrelationFunctions.findOrdinatesNum(6, lambda, omega);
        double th[] = CorrelationFunctions.theoryFuncN6((int) ordNum, sampIntrv, lambda, omega);
        double y[] = CorrelationFunctions.realFuncN6(valNum, sampIntrv, lambda, omega);
        double ord[] = CorrelationFunctions.corFuncOrdinates(y, (int) ordNum, valNum);

        double res[] = new double[y.length];
        double[] srckorr = new double[15];

        System.arraycopy(y, 0, res, 0, y.length);

        for (int i = 0; i < y.length; i++) {

            res[i] += i / 2500.0 + 0.5 * Math.sin(i / 500.0);

        }

        double x[] = new double[y.length];

        
        for (int i = 0; i < x.length; i++) {

            x[i] = i;
        }
        
        dp.setProcess(y, x);
        double mto2 = dp.getMean();
        double disp2 = dp.getDisp();
        System.out.println("MTO_I:"+mto2+" DISP_I:"+disp2);
        for (int i = 0; i < srckorr.length; i++) {
            srckorr[i] = nKorr(y, i, mto2, disp2);
        }

        dp.setProcess(res, x);

        pan.setAutoBounds(0);


        double[] fres = dp.filtrateProc(14, 0.01);
        double[] rs = new double[fres.length];
        // pan.addPlot("", "", Color.green, th);
        pan.addLinePlot(null, Color.BLUE, res);

                       dp.setProcess(fres, x);
                fres = dp.filtrateProc(14, 0.01);
        for (int i = 0; i < fres.length; i++) {

            rs[i] = res[i] - fres[i];


        }
       //   dp.setProcess(rs, x);
        
        
        pan2.addLinePlot(null, Color.BLUE, fres);
        pan2.addLinePlot(null, Color.RED, rs);
        
        double[] korr = new double[15];
        dp.setProcess(rs, x);
        double mto = dp.getMean();
        double disp = dp.getDisp();
        System.out.println("MTO:"+mto+" DISP:"+disp);
        for (int i = 0; i < korr.length; i++) {
            korr[i] = nKorr(rs, i, mto, disp);
        }
        pan3.addLinePlot(null,Color.BLACK, korr);
        pan3.addLinePlot(null,Color.ORANGE, srckorr);
        // pan.addLinePlot(null, ord);
        //pan.addPlot("", "", Color.red, ord);
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUITest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUITest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUITest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUITest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUITest().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private org.math.plot.Plot2DPanel pan;
    private org.math.plot.Plot2DPanel pan2;
    private org.math.plot.Plot2DPanel pan3;
    // End of variables declaration//GEN-END:variables
}
